import os
from dotenv import load_dotenv 

# 外部工具介绍：https://python.langchain.com/docs/integrations/tools/tavily_search/
#from langchain_community.tools.tavily_search import TavilySearchResults # 这个方法已经被废弃了
from langchain_tavily import TavilySearch

from langchain.agents import AgentExecutor, create_tool_calling_agent, tool
from langchain_core.prompts import ChatPromptTemplate
from langchain.chat_models import init_chat_model

load_dotenv(override=True)
search = TavilySearch(max_results=2)
'''
#这里是一个基础的调用
result=search.invoke("苹果2025WWDC发布会")
print(result)
'''

tools = [search]

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "你是一名助人为乐的助手，并且可以调用工具进行网络搜索，获取实时信息。"),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)

# 初始化模型
model = init_chat_model("deepseek-chat", model_provider="deepseek")

agent = create_tool_calling_agent(model, tools, prompt)

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

agent_executor.invoke({"input": "请问苹果2025WWDC发布会召开的时间是？"})